<?php
namespace application\admin\controllers;

use mz\ars\Pages;
use Yii;
use mz\controllers\AdminController as Controller;

class AboutController extends Controller
{
    # 单页类型
    protected $_type = 'about';

    #关于我们页面加载
    public function actionIndex()
    {
        $about = Pages::find()->where(['type' => $this->_type])->asArray()->one();
        return $this->render('index',compact('about'));
    }

    # 执行保存的操作
    public function actionSave()
    {
        $post = Yii::$app->request->post();

        if (empty($post))return $this->ajaxReturn(['e' => 1,'m' => '提交数据不能为空']);
        $post['type'] = $this->_type;
        $pageObj = !empty($post['id']) ? Pages::find()->where(['id' => $post['id']])->one() : new Pages();
        array_filter($post,function($key){
            return in_array($key,['title','desc','picture','content']);
        },ARRAY_FILTER_USE_KEY);

        $pageObj->attributes = $post;
        if ($pageObj->validate()){
            if ($pageObj->save())return $this->ajaxReturn(['e' => 0 , 'm' => '数据保存成功！']);
        } else {
            return $this->ajaxReturn(['e' => 0 , 'm' => '数据验证失败！']);
        }

        return $this->ajaxReturn(['e' => 1, 'm' => '很遗憾，数据保存失败~']);
    }

}